package com.sl.au.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.ReplacementDevice;

/**
 * 
 * @author XH
 * 设备管理备品备件
 *
 */
@Transactional(readOnly = true)
public interface ReplacementDeviceRepository extends JpaRepository<ReplacementDevice, String> {

	/**
	 * 根据查询条件查询数据
	 * @param key
	 * @param pageable
	 * @return
	 */
	@Query(" select r from ReplacementDevice r where r.deviceName like '%'+?1+'%' "
			+ "or r.type like '%'+?1+'%' "
			+ "or r.hostType like '%'+?1+'%' "
			+ "or r.memo like '%'+?1+'%' "
			+ "or r.material like '%'+?1+'%'")
    Page<ReplacementDevice> getReplacementDevices(String key, Pageable pageable);
	
	@Query(" select count(r) from ReplacementDevice r where r.deviceName like '%'+?1+'%' "
			+ "or r.type like '%'+?1+'%' "
			+ "or r.hostType like '%'+?1+'%' "
			+ "or r.memo like '%'+?1+'%' "
			+ "or r.material like '%'+?1+'%'")
	long getCount(String key);
	
	//利用备件名称验证是否记录>1
	@Query(" select count(r) from ReplacementDevice r where r.deviceName=?1 and r.type=?2 and r.material=?3")
	long getReCount(String deviceName,String type,String material);
	//利用备件名称找到记录
	List<ReplacementDevice>findByDeviceNameAndTypeAndMaterial(String deviceName,String type,String material);
	
}
